package com.dishes.douguo.Controller;


import com.dishes.douguo.Service.foodService;
import com.dishes.douguo.Service.productService;
import com.dishes.douguo.domain.Food;
import com.dishes.douguo.domain.Product;
import com.dishes.douguo.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Random;

@Api(tags = "菜谱信息管理")
@RestController
@RequestMapping("/dishes/douguo/food")
public class foodInfoController {
    @Autowired
    foodService service;

    @Autowired
    productService service1;

    @ApiOperation("获取菜谱信息")
    @CrossOrigin
    @GetMapping("/getFoodInfo")
    public AjaxResult getFoodInfo(Integer page){
//        System.out.println(imgId);


//        System.out.println("菜谱信息请求成功!");

        return  AjaxResult.success(service.getFoodInfo(page));
    }

    @ApiOperation("搜索名字获取菜谱和商品信息")
    @CrossOrigin
    @GetMapping("/getFoodInfoByName")
    public AjaxResult getFoodInfoByName(String Name){

        List<Food> result1 = service.getFoodInfoByName(Name);
        List<Product> result2 = service1.getProductListByName(Name);


        return  AjaxResult.doubleSuccess(result1,result2);
    }

    @ApiOperation("搜索地区获取菜谱信息")
    @CrossOrigin
    @GetMapping("/getFoodInfoByAddress")
    public AjaxResult getFoodInfoByAddress(String address){


        return  AjaxResult.success(service.getFoodInfoByAddress(address));
    }


    @ApiOperation("添加菜谱")
    @CrossOrigin
    @PostMapping ("/uploadFood")
    public AjaxResult uploadFood(Food food){
        Random random = new Random();
        int randomNumber = 1000 + random.nextInt(9000);
        String randomNumberStr = Integer.toString(randomNumber);




        String Str1 = "../imges/";
        String Str2 = food.getFoodName()+".jpg";
        service.storeFile(food.getFile(),Str2);
        String result = Str1+Str2;

         food.setImgUrl(result);
         food.setFavCount(randomNumberStr);
         service.uploadFood(food);

        return  AjaxResult.success("添加成功");
    }

    @ApiOperation("通过作者获取菜谱信息")
    @CrossOrigin
    @GetMapping("/getFoodInfoByAuthor")
    public AjaxResult getFoodInfoByAuthor(String author){


        return  AjaxResult.success(service.getFoodInfoByAuthor(author));
    }

    @ApiOperation("通过Id删除菜谱信息")
    @CrossOrigin
    @DeleteMapping ("/delFoodInfoById")
    public AjaxResult delFoodInfoById(Integer food_Id){

        service.delFoodInfoById(food_Id);

        return  AjaxResult.success("删除成功");
    }

    @ApiOperation("修改菜谱信息")
    @CrossOrigin
    @PostMapping  ("/updataFoodInfo")
    public AjaxResult updataFoodInfo(Food food){

//        System.out.println(food.getFavCount());
        String Str1 = "../imges/";
        String Str2 = food.getFoodName()+".jpg";
        service.storeFile(food.getFile(),Str2);
        String result = Str1+Str2;

        food.setImgUrl(result);


        service.updataFoodInfo(food);

        return  AjaxResult.success("修改成功");
    }S


}
